#!/usr/bin/perl -w

use warnings;
use strict;

use Test::Lono 'no_plan';
use Test::Fixture::RecipeTest;

BEGIN {
    use_ok( 'Lono::Fetcher::Client::Epicurious' );
}

use constant DATA_FILE => "$ENV{'HOME'}/projects/lono-recipe/t_data/Lono/Fetcher/Client/Epicurious/109126.xml";

my $test_file;
if ($ARGV[0]) {
    if (-r $ARGV[0]) {
        $test_file = $ARGV[0];
    }
}

$test_file ||= DATA_FILE;

my $rtest = Test::Fixture::RecipeTest->new( $test_file );

my $html = $rtest->get_html;

my $fetcher = Lono::Fetcher::Client::Epicurious->new;
isa_ok( $fetcher, 'Lono::Fetcher::Client::Epicurious' );

# Check name
is( $fetcher->_get_name( $html ), $rtest->get_name, 'Name field matches.' );

# Check description
is_same_content( $fetcher->_get_description( $html ), $rtest->get_description, 'Description field matches.' );

# Check ingredients
my @ingredients = $fetcher->_get_ingredients( $html );
my @file_ingredients = $rtest->get_ingredients;

is( $#ingredients, $#file_ingredients, 'Ingredient lists are the same size.' );
is_deeply_normalized_array( \@ingredients, \@file_ingredients, 
                            'Ingredient lists are identical.' ); 

# Check yield unit
# TODO: check yield unit

# Check yield amount
# TODO: check yield amount

# Check preparation
is_same_content( $fetcher->_get_preparation( $html ), $rtest->get_preparation, 
                 'Preparation field matches.'); 

# TODO: check active time
# TODO: check total time
# TODO: check nutrition info

my $response = $fetcher->handle($html);
isa_ok( $response, 'Lono::Fetcher::Response' );

my $recipe = $response->get_recipe;
isa_ok( $recipe, 'Lono::Recipe' );

# Test the fields through the recipe class

is_same_content( $recipe->get_description, $rtest->get_description, 
    'Lono::Recipe\'s description matches.' );

is_same_content( $recipe->get_preparation, $rtest->get_preparation, 
    'Lono::Recipe\'s preparation matches.' );

# TODO: check ingredients

# XXX: test exception flow...
